<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>


    let nums =[-1, 10, 1, 2, -1, -4];
    // 超出时间限制
    let threeSum1 = function (nums) {
        let temp = [];
        // n * (n-1) * (n-2)
        for (let i = 0; i < nums.length - 2; i++) {
            for (let j = i + 1; j < nums.length-1; j++) {
                for(let k = j+1;k<nums.length;k++){
                    if(nums[i] + nums[j] + nums[k] === 0){
                        temp.push([nums[i],nums[j],nums[k]].sort().toString())
                    }
                }
            }
        }

        let temp2 = [];
        // n
        for(let i = 0; i<temp.length; i++){
            if(temp2.indexOf(temp[i])<0){
                temp2.push(temp[i])
            }
        }
        // n*n
        return temp2.map((item)=>{
            console.log(item);
            return item.split(',').map((it)=>{
                console.log(it);
                return Number(it)
            })
        })
    };

    // n*(n-1)*(n-2)
    let threeSum2 = function (nums) {
        let temp = [];
        let res = [];
        for (let i = 0; i < nums.length - 2; i++) {
            for (let j = i + 1; j < nums.length-1; j++) {
                for(let k = j+1;k<nums.length;k++){
                    if(nums[i] + nums[j] + nums[k] === 0){
                        let str = [nums[i],nums[j],nums[k]].sort().toString();
                        if(temp.indexOf(str)<0){
                            temp.push(str);
                            res.push([nums[i],nums[j],nums[k]])
                        }

                    }
                }
            }
        }
        return res;
    };

    let threeSum3 = function (nums) {
        let temp = [];
        let res = [];
        for (let i = 0; i < nums.length - 1; i++) {
            for (let j = i + 1; j < nums.length; j++) {
                let num = 0 - nums[i] - nums[j];
                let index = nums.indexOf(num)
                if(index>-1 && index !== i && index !==j ){
                    let str = [nums[i],nums[j],num].sort().toString();
                    if(temp.indexOf(str)<0){
                        temp.push(str);
                        res.push([nums[i],nums[j],num])
                    }
                }

            }
        }
        return res;
    };

    let threeSum4 = function (nums) {
        /**
         * 判断原始数组是否包含0
         * @returns {boolean} true 包含0 反之 不包含
         */
        let includeZero  = nums.includes(0);
        /**
         * 排序 从小到大
         * @param a
         * @param b
         * @returns {number}
         */
        const sort = function (a,b) {
            return a-b
        };
        // 判断原始数组是否包含 0 然后排序
        if(includeZero){
            nums = nums.sort((a,b)=>{
                return sort(a,b)
            });
        }else {
            nums.push(0);
            nums = nums.sort((a,b)=>{
                return sort(a,b)
            });
        }
        let zeroIndex = nums.indexOf(0); // 0 出现的位置
        let beforeZeroArr = nums.slice(0,zeroIndex);// 小于0的部分
        let otherArr = nums.slice(zeroIndex,nums.length); // 大于 -1 的部分

        let beforeLen = beforeZeroArr.length;// 0之前的数组长度
        let otherLen = otherArr.length; // 0 之后的长度
        // 设置开始的下标
        const setStartIndex = () =>{


        }

    };
    threeSum4(nums)
</script>
</html>
